﻿using System;
using System.Text;
using OpenGameEngine.Base;
using System.Windows.Forms;
using OpenGameEngine.Input;

namespace OpenGameEngine.GameObjects
{
    /// <summary>
    /// This is the base class for all objects that will be created with OGE
    /// </summary>
    public abstract class GameObject
        : Object
    {
        protected internal Guid UniqueID { get; private set; }

        private Vector2 _position;
        /// <summary>
        /// Gets or sets the position
        /// </summary>
        public virtual Vector2 Position 
        {
            get { return _position; }
            set { _position = value; }
        }

        /// <summary>
        /// Gets or sets the X position
        /// </summary>
        public virtual float X
        {
            get { return Position.X; }
            set { _position.X = value; }
        }

        /// <summary>
        /// Gets or sets the Y position
        /// </summary>
        public virtual float Y
        {
            get { return Position.Y; }
            set { _position.Y = value; }
        }

        public virtual float Scale { get; set; }

        /// <summary>
        /// Gets or sets the visibility
        /// </summary>
        public virtual Boolean Visible { get; set; }

        /// <summary>
        /// Gets or sets if the object is enabled
        /// </summary>
        public virtual Boolean Enabled { get; set; }

        /// <summary>
        /// Gets if the object is loaded
        /// </summary>
        public virtual Boolean IsLoaded { get; private set; }

        /// <summary>
        /// Gets or sets the name of this object
        /// </summary>
        public virtual String Name { get; set; }

        /// <summary>
        /// Gets or sets if this object accepts mouse input
        /// </summary>
        public virtual Boolean AcceptsMouseInput { get; set; }

        /// <summary>
        /// Gets or sets if this object accepts keyboard input
        /// </summary>
        public virtual Boolean AcceptsKeyboardInput { get; set; }

        private bool blnCurrKb;
        private bool blnPrevKb;


        public GameObject()
        {
            UniqueID = Guid.NewGuid();
            Position = Vector2.Zero;
            Name = "GameObject.{" + UniqueID.ToString() + "}";
            Scale = 1f;
            Enabled = true;
        }

        public virtual void Load(GameCoreEventArgs e)
        {
            IsLoaded = true;
        }

        public virtual void Draw(DrawEventArgs e)
        {

        }

        public virtual void Update(GameCoreEventArgs e)
        {
            if (AcceptsKeyboardInput)
            {
                KeyboardStateInfo kb = e.Core.CurrentKeyboardStateInfo;
                KeyboardStateInfo prevKb = e.Core.PreviousKeyboardStateInfo;

                foreach (Keys key in Globals.KeyNames)
                {
                    blnCurrKb = kb[key];
                    blnPrevKb = prevKb[key];

                    if (blnCurrKb)
                        KeyDown(e, key);
                    if (blnCurrKb && !blnPrevKb)
                    {
                        KeyPress(e, key);
                    }
                    else if (blnPrevKb && !blnCurrKb)
                    {
                        KeyUp(e, key);
                    }
                }
            }
        }

        public virtual void KeyDown(GameCoreEventArgs e, Keys key)
        {

        }

        public virtual void KeyUp(GameCoreEventArgs e, Keys key)
        {

        }

        public virtual void KeyPress(GameCoreEventArgs e, Keys key)
        {

        }

        public virtual void MouseDown(GameCoreEventArgs e, MouseButtons button)
        {

        }

        public virtual void MouseUp(GameCoreEventArgs e, MouseButtons button)
        {

        }

        public virtual void MouseClick(GameCoreEventArgs e, MouseButtons button)
        {

        }

        public virtual void MouseEnter(GameCoreEventArgs e)
        {

        }

        public virtual void MouseLeave(GameCoreEventArgs e)
        {

        }
    }
}
